//
//  UserCenterViewController.m
//  OrderMeal
//
//  Created by 宏创 on 14-2-25.
//  Copyright (c) 2014年 周浩. All rights reserved.
//

#import "UserCenterViewController.h"
#import "AFNetworking.h"
#import "UserInfoViewController.h"
#import "MBProgressHUD.h"
#import "UIView+Toast.h"
@interface UserCenterViewController (){
    MBProgressHUD *hud;
    NSString *warning;
}
@property (strong,nonatomic)NSDictionary *retData;
@end

@implementation UserCenterViewController
@synthesize retData = _retData;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
}


#pragma mark - Navigation
//传递数据
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"登录"]) {
        UserInfoViewController *userInfoVC=segue.destinationViewController;
        userInfoVC.retData = self.retData;
    }
}

- (IBAction)loginAction:(UIButton *)sender {
    
    if ([self checkInput]) {
        hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeIndeterminate;
        hud.labelText = @"登陆中";
        [self.view addSubview:hud];
        [self loginWithEmail:self.userName.text
                    password:self.password.text];
    }
}
- (IBAction)registerAction:(id)sender {
    [self performSegueWithIdentifier:@"注册" sender:nil];
}

-(BOOL)checkInput
{
    if ([self.userName.text length]==0) {
        warning =@"请输入用户名！";
        return NO;
        [self.view makeToast:warning duration:2.0 position:@"center"];
    }
    if ([self.password.text length]==0) {
        warning =@"请输入密码";
        return NO;
        [self.view makeToast:warning duration:2.0 position:@"center"];
    }
    
    return YES;
}


-(void)loginWithEmail:(NSString *)email
                password:(NSString *)password
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setObject:email forKey:@"username"];
        [userDefaults setObject:password forKey:@"password"];
        [userDefaults synchronize];
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    NSDictionary *parameters= @{
                                 @"username" : email,
                                 @"password" : password
                               };
    
    [manager GET:login_url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
       
        NSNumber *status=responseObject[@"success"];
        
        if ([status isEqualToNumber:@1]) {
            self.retData = responseObject[@"retData"];
            [userDefaults setObject:self.retData[@"id"] forKey:@"memberID"];
            [userDefaults setObject:self.retData[@"phone"] forKey:@"phone"];
            [userDefaults setObject:self.retData[@"address"] forKey:@"address"];
            [userDefaults setObject:[NSString stringWithFormat:@"%@",self.retData[@"building"][@"id"]] forKey:@"buildingID"];
            [userDefaults setObject:self.retData[@"building"][@"name"] forKey:@"buildingName"];
            [userDefaults synchronize];
            NSLog(@"buildingName--->%@",[userDefaults objectForKey:@"buildingName"]);
            [hud removeFromSuperview];
           [self performSegueWithIdentifier:@"登录" sender:nil];
        }else{
           
           warning = responseObject[@"msg"];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [self.view makeToast:warning duration:2.0 position:@"center"];
           warning =@"网络出错";
    }];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
    self.userName.text = [userDefaults objectForKey:@"username"];
    self.password.text=[userDefaults objectForKey:@"password"];}
    
@end
